package com.lottery.football.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.Span;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.springframework.stereotype.Service;

import com.lottery.football.mapper.ScheduleMapper;
import com.lottery.football.pojo.Schedule;
import com.lottery.football.pojo.Team;
import com.lottery.football.service.ScheduleService;
import com.lottery.football.service.TeamService;
import com.lottery.football.util.ContentParser;

@Service(value="scheduleService")
public class ScheduleServiceImpl implements ScheduleService
{
	private Logger logger = Logger.getLogger(ScheduleServiceImpl.class);
	
	@Resource(name="scheduleMapper")
	private ScheduleMapper scheduleMapper;
	
	@Resource(name="teamService")
	private TeamService teamService;

	/*
	 * 获取某一期的对阵表 (non-Javadoc)
	 * 
	 * @see
	 * com.lottery.football.service.ScheduleService#getSchedule(String html)
	 */
	@Override
	public List<Schedule> getSchedules(int issueNo, String html)
	{
		//String url = PropertiesUtil.get("schedule.url");
		//String html = Spiders.get(url + issueNo, Constants.GB2312);

		// 查找属性table值为_blank的a标签
		TableTag table = ContentParser.parseTag(html, TableTag.class, "id", "data_table");
		logger.debug("---------- vs table start ----------");
		logger.debug(table.toHtml());
		logger.debug("---------- vs table end ----------");

		// 14场比赛
		NodeList trList = table.searchFor(TableRow.class, false);
		List<Schedule> schedules = new ArrayList<Schedule>(14);
		for (int i = 1; i <= 14; i++)
		{
			TableRow tr = (TableRow) trList.elementAt(i);
			logger.debug("------------ tr start ---------------");
			logger.debug(tr.toHtml());
			logger.debug("------------ tr end ---------------");
			Schedule s = this.getSchedule(tr);
			s.setIssueNo(issueNo);
			schedules.add(s);
		}

		return schedules;
	}

	private Schedule getSchedule(TableRow tr)
	{
		try
		{
			NodeList tdList = tr.searchFor(TableColumn.class, false);
			Schedule s = new Schedule();
			Team hostTeam = new Team();
			Team guestTeam = new Team();

			TableColumn orderTd = (TableColumn) tdList.elementAt(0);
			TableColumn typeTd = (TableColumn) tdList.elementAt(1);
			TableColumn timeTd = (TableColumn) tdList.elementAt(2);
			TableColumn vsTd = (TableColumn) tdList.elementAt(3);
			TableColumn rateTd = (TableColumn) tdList.elementAt(5);

			s.setOrderNo(Integer.parseInt(orderTd.getStringText()));
			s.setType(((LinkTag) typeTd.childAt(0)).getStringText());

			String timeStr = new Date().getYear() + "-" + timeTd.childAt(0).getText();
			//s.setStartTime(new Date(timeStr));

			// 取球队名，ID
			NodeList spanList = vsTd.searchFor(Span.class, false);
			LinkTag hostLink = (LinkTag) ((Span) spanList.elementAt(1)).childAt(0);
			String hostDetailPage = hostLink.getAttribute("href");
			String hostId = hostDetailPage.substring(hostDetailPage.indexOf("teamid-") + 7);
			logger.debug("-----" + hostDetailPage);
			logger.debug("-----" + hostId);

			hostTeam.setCnName(hostLink.getStringText().replaceAll("&nbsp;", ""));
			hostTeam.setDetailPage(hostDetailPage);
			hostTeam.setId(Integer.parseInt(hostId));

			LinkTag guestLink = (LinkTag) ((Span) spanList.elementAt(3)).childAt(0);
			String guestDetailPage = guestLink.getAttribute("href");
			String guestId = guestDetailPage.substring(hostDetailPage.indexOf("teamid-") + 7);
			logger.debug("-----" + guestDetailPage);
			logger.debug("-----" + guestId);

			guestTeam.setCnName(guestLink.getStringText().replaceAll("&nbsp;", ""));
			guestTeam.setDetailPage(guestDetailPage);
			guestTeam.setId(Integer.parseInt(guestId));

			s.setHostTeam(hostTeam);
			s.setGuestTeam(guestTeam);

			// 赔率
			String winRateStr = ((Span) rateTd.childAt(0)).getStringText();
			String drawRateStr = ((Span) rateTd.childAt(1)).getStringText();
			String lossRateStr = ((Span) rateTd.childAt(2)).getStringText();

			s.setWinRate(Float.parseFloat(winRateStr));
			s.setDrawRate(Float.parseFloat(drawRateStr));
			s.setLossRate(Float.parseFloat(lossRateStr));

			return s;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	/*
	 * 保存对阵表 (non-Javadoc)
	 * 
	 * @see
	 * com.lottery.football.service.ScheduleService#insert(Schedule)
	 */
	@Override
	public int insert(Schedule schedule)
	{
		if (schedule == null)
		{
			return 0;
		}
		try
		{
			// 先保存球队信息
			teamService.insert(schedule.getHostTeam()); 
			teamService.insert(schedule.getGuestTeam()); 
			return scheduleMapper.insert(schedule);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<Integer> findUnperfectIssueNo(){
		return scheduleMapper.findUnperfectIssueNo();
	}

}
